// TOWN SCRIPT
//    Town 2: Fort Bluebird

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

begintownscript;

variables;

int i,j;

body;

beginstate INIT_STATE;
// Called upon entering

set_character_pose(15,14);

set_crime_tolerance(2);

set_name(6,"Ivan");
set_level(6,100);
set_name(7,"Jessica");
set_level(7,15);
set_name(8,"Mage");
set_name(9,"Rita");
set_level(9,6);
set_name(10,"Mage");
set_name(11,"Howie");
set_name(12,"Chris");
set_name(13,"'Chelly");
set_name(14,"Roberts");
set_name(15,"Sleeping Soldier");
set_name(17,"Dillian");
set_name(18,"Anni");
set_name(19,"Archer");
set_name(21,"Researcher Blake");
set_level(21,7);
set_name(22,"Archer");
set_name(28,"Archer");
set_name(29,"Captain Morley");

if(get_flag(2,4) == 1) {
	erase_char(7);
}

if(get_flag(2,18) == 1) {
	erase_char(12);
}

if(get_flag(2,7) == 0) {
		message_dialog("You enter Fort Bluebird, the tower that houses the large experimental, trans-dimensional portal. Even from the entrance you can feel magic heavy in the air.","There is also something else though, like a low hum, that is constantly distracting you. Must be a side-effect of the portal.");
		set_flag(2,7,1);
} 

if(get_flag(1,13) == 1) { //If second quest is go, set state to clear town etc.
	set_state_continue(14);
}

if((get_flag(1,13) == 1) && (get_flag(50,1) == 0)) {
	activate_hidden_group(1);
}



break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
// Called every turn.


//if((get_current_tick() % 55) == 0) {
//	print_str("Your mind goes all foggy due to the humming noise.");
//	set_party_status(13,25,4);
//}


break;

beginstate 11; //Cache Message
	if(get_flag(2,3) == 0) {
		message_dialog("This small storeroom contains valuable supplies that are to be used in case of an emergency. There are potions and a wickedly sharp dagger. You guess that nobody would mind you taking them.","");
		set_flag(2,3,1);
	}
break;

beginstate 12; //Stop them going to Portal Room too early
	if(get_flag(1,13) == 0) {
		message_dialog("You try and get past the huge soldier that is standing guard here. You don't get very far. As soon as he sees what you're doing, he shouts an alarm and proceeds to stand in your way.","_Don't go trying that, 'cus you won't get past me, got it?_");
		block_entry(1);
	}
break;

beginstate 13; //Portal Panels
	message_dialog("You glance at the control panel. It is covered with flashing lights, buttons, switches and sliding levers. None of it is labeled. Rather than monkey around with the panel and risk a quick death, you leave it be.","");
break;

beginstate 14; //Clear Chars, set terrain. (Changing town when re-entering)

	if(get_flag(2,8) == 0) {
		message_dialog("You return to Fort Bluebird. Everything is deathly silent, except for a humming sound. The sound is more pronounced then before, and is stopping you concentrating.","Just as you wonder what could be causing it, you see Captain Morley, the man who interrupted your meeting with Commander Hollie, up ahead.");
		set_flag(2,8,1);
}
	erase_char(6);
	flip_terrain(38,32);
	put_field_on_space(20,11,2);
	flip_terrain(47,26);
	flip_terrain(47,25);
	put_item_on_spot(38,32,450);
	
	if(get_flag(50,1) == 0) {
		activate_hidden_group(1);
	}
break;

beginstate 15; //Dead guard at Portal Room stairs
	message_dialog("This corpse is the remains of the huge soldier that was standing guard last time you were here. He now lies with his throat slit, and several long gashes down his face.","You should have no problem getting past him now.");
break;

beginstate 16; //Jess's shelves
	message_dialog("These shelves contain all manner of books, journals and sheets of paper. However, you can't use any of them - as soon as you reach for one it disappears. It's clear that somebody doesn't want you to read them.","Alas, the magic is too strong for you to counter. Dejected, you leave the shelves be.");
break;

beginstate 17; //North Library
	if(get_flag(2,10) == 0) {
		message_dialog("You enter Fort Bluebird's northern library. The smell of aging books and paper is heavy on the air. Looking around, your sense of greed kicks in, and you imagine the power these shelves might contain.","You grab the first book off the nearest shelf, and are dismayed to see that it is a work of fiction. If there are any spellbooks here, you'll have to search hard for them.");
		set_flag(2,10,1);
	}
break;

beginstate 18; //Give party one level of Bolt Of Fire
	if (get_flag(2,11) != 0) {
		message_dialog("You skim over the book again, but there is nothing left to learn. You have learned everything about Bolt Of Fire that this book has to teach.","");
		end(); 
	}
	message_dialog("You sit down in front of the large tome. It is a spellbook, detailing various rituals. You flick through in the hope of finding something useful.","Your work pays off. At the end, you find a section on casting the spell Bolt of Fire with more efficiency.");
	j = 0;
	i = 0;
	while (i <= 3) {
		if (get_stat(i,11) >= 1) {
			change_spell_level(i,0,0,1);
		print_named_str(i,"learns a level of Bolt Of Fire.");
		j = 1;
		}
	i = i + 1;
	}
	if (j == 0)
		message_dialog("Unfortunately, no one in your party is skilled enough to learn the spell.","");
	else
		set_flag(2,11,1);
break;

beginstate 19; //Give party one level of Light
	if (get_flag(2,12) != 0) {
		message_dialog("You skim over the book again, but there is nothing left to learn. You have learned everything about Light that this book has to teach.","");
		end(); 
	}
	message_dialog("You sit down in front of the large tome. It is a spellbook, detailing various rituals. You flick through in the hope of finding something useful.","Your work pays off. At the end, you find a section on casting the spell Light with more efficiency.");
	j = 0;
	i = 0;
	while (i <= 3) {
		if (get_stat(i,11) >= 1) {
			change_spell_level(i,0,1,1);
		print_named_str(i,"learns a level of Light.");
		j = 1;
		}
	i = i + 1;
	}
	if (j == 0)
		message_dialog("Unfortunately, no one in your party is skilled enough to learn the spell.","");
	else
		set_flag(2,12,1);
break;

beginstate 20; //Spell room
	if(get_flag(2,13) == 0) {
		message_dialog("This damp, chilly room is home to tons and tons of magical tomes. There are bookshelf after bookshelf of exciting treatises and theories, and two very inviting tomes laying open straight ahead.","It was no wonder that this room was guarded so well.");
		set_flag(2,13,1);
	}
break;

beginstate 21; //Stop them getting spells too early
	if(has_special_item(1) == 0) {
		message_dialog("As you step on the rune, glowing green tendrils wrap themselves around you, seemingly searching for something. Unfortunately, they dont find what they are looking for, and create a barrier in front of you.","You won't be able to go on this way.");
		block_entry(1);
	} else {
		if(get_flag(2,15) == 0) {
			message_dialog("As you step on the rune, glowing green tendrils wrap themselves around you, seemingly searching for something. They dig into pockets, and through your packs.","Then they wrap around the key you got from Jessica, and the fade away. You should be able to pass these runes now.");
			set_flag(2,15,1);
			block_entry(0);
		} 
	}
break;

beginstate 22; //Storeroom Greeting
	if(get_flag(2,14) == 0) {
		message_dialog("As you enter this storeroom, you are instantly hit by the smell of rotting flesh. Looking around, you soon see why. What can only be a dead body is lying on the floor surrounded by a pool of blood.","This must be where the killing took place. Maybe there are some clues as to who the murderer might be near the corpse.");
		set_flag(2,14,1);
	}
break;

beginstate 23; //Searched Shaun's body
	message_dialog("This poor soul was stabbed several times in the back. he must have been looking for something in the storeroom, and was attacked from behind. Holding your nose, you search him for any clues as to who his attacker might have been.","Alas, you find nothing incriminating on his person, or on the ground around him. You aren't going to find anything here. You should go back and tell Jessica.");
	set_flag(2,9,1);
break; 

beginstate 24; //Mediatation cells
	if(get_flag(2,16) == 0) {
		message_dialog("This empty meditation room is decorated to look like the outdoors. A bright light shines down from a magically enhanced ceiling, which has been made to look like the sky - limitless and deep, deep blue.","The grass is soft underfoot,the air sweet. You instantly feel at ease in here, and take a moment to relax.");
		set_flag(2,16,1);
	}
break;

beginstate 25; //Jess's doors
	if((get_flag(2,17) == 0) && (get_flag(1,13) == 1))  {
		message_dialog("As you approach Jessica's office, you notice that her doors are open. That alone isn't unusual. However, you can hear her faintly muttering things to herself and banging drawers.","You wonder if she's alright.");
		set_flag(2,17,1);
	}
break;

beginstate 26; //Stop them traipsing around after shade is gone
	if(get_flag(2,5) == 1) {
		message_dialog("You should go back to Fort Orchid as soon as possible. Lingering around here isn't a good idea, especially considering the state of the magi.","");
		block_entry(1);
	}
break;

beginstate 27;
	message_dialog("I know it's over, and still I claim, I don't know where else I can go...","(And yes, you monkey. This is a poorly hidden easter egg. Try and find the other one.)");
break;

beginstate 28; //South Library
	if(get_flag(2,20) == 0) {
		message_dialog("You enter Fort Bluebird's southern library, the smaller of the two libraries in the Fort, and thus the island. The air in here is stuffy - the Fort could really use a window or two.","You look around, and notice that the books on the shelves are all articles on the art of teleportation. There's probably little here you can use.");
		set_flag(2,20,1);
	}
break;

beginstate 29; //Bookshelf in S Library
	if(get_flag(2,21) == 0) {
		message_dialog("This bookshelf, like all the others in the room, is filled with thick tomes on the subject of teleportation. However, on further inspection, you also find a scroll tube wedged in at one end.","");
		set_flag(2,21,1);
	}
break;

beginstate 30; //Lab greeting
	if(get_flag(2,22) == 0) {
		message_dialog("No magical fort in the Empire is complete without the obligatory laboratory, and Fort Blubeird is no exception. As you enter, you are taken aback by the smell of ozone, and the staggering amount of paper on the shelves.","You also spot a pit, heavily splattered in blood, set into the ground. There doesn't seem to be much happening in here - only one researcher is present at the moment.");
		set_flag(2,22,1);
	}
break;

beginstate 31;
	if(get_flag(2,23) == 0) {
		reset_dialog();
		add_dialog_str(0,"You enter the portal room. There is nobody around, yet you can faintly hear whipsering coming from all around you. As you take another step in, the huge portal in front of you grabs your attention - it is changing colour.",0);
		add_dialog_str(1,"As you look closer, you notice that it isn't just that - something is coming through it!",0);
		run_dialog(1);
		
		force_view_center(30,32);
		force_instant_terrain_redraw();
		put_boom_on_space(28,32,2,0);
		run_animation_sound(10);
		
		activate_hidden_group(3);
		force_instant_terrain_redraw();
		
		message_dialog("Standing before you is the shade of a young child. He glows softly, and looks at you. Then, he holds his hands out, and begins talking to you.","");
		
		text_bubble_on_char(31,"Oh! You have come.");		
		force_instant_terrain_redraw();
		pause(20);
		
		text_bubble_on_char(31,"");	
		text_bubble_on_char(31,"I am sorry for what");		
		force_instant_terrain_redraw();
		pause(20);
		
		text_bubble_on_char(31,"");		
		text_bubble_on_char(31,"I've done. I know it");		
		force_instant_terrain_redraw();
		pause(20);
		
		text_bubble_on_char(31,"");		
		text_bubble_on_char(31,"was bad. But I had to be");		
		force_instant_terrain_redraw();
		pause(20);
		
		text_bubble_on_char(31,"");		
		text_bubble_on_char(31,"free! If you will");		
		force_instant_terrain_redraw();
		pause(20);
		
		text_bubble_on_char(31,"");		
		text_bubble_on_char(31,"let me explain, I'm");		
		force_instant_terrain_redraw();
		pause(20);
	
		text_bubble_on_char(31,"");		
		text_bubble_on_char(31,"sure you'll understand.");		
		force_instant_terrain_redraw();
		pause(20);
		
		text_bubble_on_char(31,"");		
		text_bubble_on_char(31,"What do you say?");		
		force_instant_terrain_redraw();
		pause(20);
		text_bubble_on_char(31,"");		
		force_instant_terrain_redraw();
			
		reset_dialog();
		add_dialog_str(0,"Suddenly, Morley bursts into the room. _Ah you got here before..._ he starts. Then he sees the shade, and immediately draws his sword. Then he turns to you.",0);
		add_dialog_str(1,"_What are you waiting for! Let's attack it!_",0);
		add_dialog_choice(0,"No, wait! Give me a second...");
		add_dialog_choice(1,"Okay, let's go!");
		if(run_dialog(0) == 2) {
			activate_hidden_group(2);
			set_flag(2,23,1);
			end();
		} else {
			message_dialog("Morley looks at you for a minute, but holds his hand.","_Okay, I'll give you a chance. I hope you know what you're doing though - I've found out that this monster is the murderer..._");
			message_dialog("You turn back to the shade. He begins speaking again. _Thank you. Let me tell you who I am, and why this happened._","You think you see him smile.");
			
			reset_dialog();
			add_dialog_str(0,"_My name is Ebhert. I used to live on this island long ago, with my friends and family. Then something bad happened. Mummy didn't tell me what. And then everything went funny.",0);
			add_dialog_str(1,"_Everyone started to move into the caves near here, and did something to the crystals. Then they all went to sleep, and I couldn't wake them up. Then, one day, I was really hungry, and so tired, and I, I went to sleep too.",0);
			add_dialog_str(2,"_Then, I woke up. Everyone was still around me. I could even see myself. I was scared, and alone. I was the only kid on the island - there weren't many of us. I wanted mummy, but I couldn't leave the caves to find her.",0);
			add_dialog_str(3,"_So I waited a long long time. And then new people came here. They started taking the crystals. And made this big door._ He waves at the portal.",0);
			run_dialog(1);
			
			text_bubble_on_char(31,"");		
			text_bubble_on_char(31,"Does this make sense?");		
			force_instant_terrain_redraw();
			pause(20);
			
			text_bubble_on_char(31,"");		
			text_bubble_on_char(31,"When I saw that the");		
			force_instant_terrain_redraw();
			pause(20);
		
			text_bubble_on_char(31,"");		
			text_bubble_on_char(31,"big door might let me");		
			force_instant_terrain_redraw();
			pause(20);
		
			text_bubble_on_char(31,"");		
			text_bubble_on_char(31,"leave, I started to");		
			force_instant_terrain_redraw();
			pause(20);
			
			text_bubble_on_char(31,"");		
			text_bubble_on_char(31,"make the people here");		
			force_instant_terrain_redraw();
			pause(20);
		
			text_bubble_on_char(31,"");		
			text_bubble_on_char(31,"do things for me.");		
			force_instant_terrain_redraw();
			pause(20);
		
			text_bubble_on_char(31,"");		
			force_instant_terrain_redraw();
			
			reset_dialog();
			add_dialog_str(0,"He lowers his head. _I had to kill a man, called Shaun, I think. He was going to close the door forever, he said. I'm sorry. But I am free now, and I can leave..._",0);
			add_dialog_str(1,"_Can't I?_",0);
			add_dialog_choice(0,"Yes, go on. You were only trying to break free.");
			add_dialog_choice(1,"No. I'm sorry, but you killed someone. You must pay.");
			if(run_dialog(1) == 2) {
				activate_hidden_group(2);
				set_flag(2,23,1);
				end();
			} else {
				message_dialog("The shade lowers his head. _Thank you. Let me give you a present, before I leave.","He looks up at you, and mutters something. As he does, you feel wiser, more alert.");
				alter_stat(1000,2,1);
 
				message_dialog("Morley looks furious. _You're just letting it go? Not on my watch!_ He lunges forward with his sword, but too late - Ebhert has already vanished.","Morley is furious. _I'm off to Fort Orchid. I suggest you come too._ And he leaves. Now that you have let Ebhert free, you should go and tell Commander Hollie.");
				set_flag(2,2,1);
				set_flag(2,5,1);
				erase_char(31);
				set_flag(2,23,1);
			}
		} else {
			end();
		}
	}
break;

beginstate 32; //Morley warning
	if((get_flag(1,13) == 1) && (get_flag(50,1) == 0)) {
		message_dialog("Before you enter the fort, you should talk to Captain Morley to see what he wants you to do.","");
		block_entry(1);
	}
break;